/**
 * ...
 * @author Krzysztof Rozalski
 */

package hapi.physics;
import hapi.math.Vector2D;
import hapi.math.Additive;
import hapi.math.Multiplicative;

class Acceleration implements Additive<Acceleration,Acceleration>, implements Multiplicative<Acceleration,Float> {
	
	var metersPerSqrSecond:Float;
	var direction:Vector2D;
	
	public static function zero() {
		return new Acceleration(0.0, new Vector2D(0., 0.));
	}
	
	public function new(metersPerSqrSecond, direction) {
		this.metersPerSqrSecond = metersPerSqrSecond;
		this.direction = direction;
	}
	
	public function velocityChangeAfter(t:Time):Velocity {
		return new Velocity(metersPerSqrSecond * t.seconds, direction.clone());
	}
	
	public function applyTo(v:Velocity, t:Time):Velocity {
		return v.add(velocityChangeAfter(t));
	}
	
	public function add(acc:Acceleration):Acceleration {
		direction.mult(metersPerSqrSecond).plus(acc.direction.multNew(acc.metersPerSqrSecond));
		metersPerSqrSecond = direction.getLength();
		direction.normalize();
		return this;
	}
	
	public function substract(acc:Acceleration):Acceleration {
		direction.mult(metersPerSqrSecond).minus(acc.direction.multNew(acc.metersPerSqrSecond));
		metersPerSqrSecond = direction.getLength();
		direction.normalize();
		return this;
	}
	
	public function multiplyBy(scalar:Float):Acceleration {
		metersPerSqrSecond *= scalar;
		return this;
	}
	
	public function divideBy(scalar:Float):Acceleration {
		return multiplyBy(1 / scalar);
	}
	
	public function isZero():Bool {
		return metersPerSqrSecond == 0.0;
	}
	
	public function toString():String {
		return metersPerSqrSecond + " mpss";
	}
}